home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 13 / CU Amiga Magazine's Super CD-ROM 13 (1997)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1997-08].iso / CUCD / Graphics / Ghostscript / src / libpng / pngtest.c < prev    next >
C/C++ Source or Header  |  1996-06-05  |  5KB  |  222 lines

  1. /* pngtest.c - a simple test program to test libpng
  2.  
  3.    libpng 1.0 beta 3 - version 0.89
  4.    For conditions of distribution and use, see copyright notice in png.h
  5.    Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  6.    May 25, 1996
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "png.h"
  12.  
  13. #ifdef __TURBOC__
  14. #include <mem.h>
  15. #endif
  16.  
  17. /* defined so I can write to a file on gui/windowing platforms */
  18. /*  #define STDERR stderr  */
  19. #define STDERR stdout   /* for DOS */
  20.  
  21. /* input and output filenames */
  22. #ifdef RISCOS
  23. char *inname = "pngtest_pn";
  24. char *outname = "pngout_png";
  25. #else
  26. char *inname = "pngtest.png";
  27. char *outname = "pngout.png";
  28. #endif
  29.  
  30. char inbuf[256], outbuf[256];
  31.  
  32. int main(int argc, char *argv[])
  33. {
  34.    FILE *fpin, *fpout;
  35.    png_structp read_ptr;
  36.    png_structp write_ptr;
  37.    png_infop info_ptr;
  38.    png_infop end_info;
  39.    png_bytep row_buf;
  40.    png_byte *near_row_buf;
  41.    png_uint_32 rowbytes;
  42.    png_uint_32 y;
  43.    int channels, num_pass, pass;
  44.  
  45.    row_buf = (png_bytep)NULL;
  46.    near_row_buf = (png_byte *)NULL;
  47.  
  48.    fprintf(STDERR, "Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
  49.  
  50.    if (strcmp(png_libpng_ver, PNG_LIBPNG_VER_STRING))
  51.    {
  52.       fprintf(STDERR,
  53.          "Warning: versions are different between png.h and png.c\n");
  54.       fprintf(STDERR, "  png.h version: %s\n", PNG_LIBPNG_VER_STRING);
  55.       fprintf(STDERR, "  png.c version: %s\n\n", png_libpng_ver);
  56.    }
  57.  
  58.    if (argc > 1)
  59.      inname = argv[1];
  60.  
  61.    if (argc > 2)
  62.      outname = argv[2];
  63.  
  64.    if (argc > 3)
  65.    {
  66.      fprintf(stderr, "usage: %s [infile.png] [outfile.png]\n", argv[0]);
  67.      exit(1);
  68.    }
  69.  
  70.    fpin = fopen(inname, "rb");
  71.    if (!fpin)
  72.    {
  73.       fprintf(STDERR, "Could not find input file %s\n", inname);
  74.       return 1;
  75.    }
  76.  
  77.    fpout = fopen(outname, "wb");
  78.    if (!fpout)
  79.    {
  80.       fprintf(STDERR, "Could not open output file %s\n", outname);
  81.       fclose(fpin);
  82.       return 1;
  83.    }
  84.  
  85.    read_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (void *)NULL,
  86.       (png_error_ptr)NULL,  (png_error_ptr)NULL);
  87.    write_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (void *)NULL,
  88.       (png_error_ptr)NULL, (png_error_ptr)NULL);
  89.    info_ptr = png_create_info_struct(read_ptr);
  90.    end_info = png_create_info_struct(read_ptr);
  91.  
  92.    if (setjmp(read_ptr->jmpbuf))
  93.    {
  94.       fprintf(STDERR, "libpng read error\n");
  95.       png_destroy_read_struct(&read_ptr, &info_ptr, &end_info);
  96.       png_destroy_write_struct(&write_ptr, (png_infopp)NULL);
  97.       fclose(fpin);
  98.       fclose(fpout);
  99.       return 1;
  100.    }
  101.  
  102.    if (setjmp(write_ptr->jmpbuf))
  103.    {
  104.       fprintf(STDERR, "libpng write error\n");
  105.       png_destroy_read_struct(&read_ptr, &info_ptr, &end_info);
  106.       png_destroy_write_struct(&write_ptr, (png_infopp)NULL);
  107.       fclose(fpin);
  108.       fclose(fpout);
  109.       return 1;
  110.    }
  111.  
  112.    png_init_io(read_ptr, fpin);
  113.    png_init_io(write_ptr, fpout);
  114.  
  115.    png_read_info(read_ptr, info_ptr);
  116.    png_write_info(write_ptr, info_ptr);
  117.  
  118.    if ((info_ptr->color_type & PNG_COLOR_TYPE_PALETTE)==PNG_COLOR_TYPE_PALETTE)
  119.       channels = 1;
  120.    else
  121.       channels = 3;
  122.    if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  123.       channels++;
  124.  
  125.    rowbytes = ((info_ptr->width * info_ptr->bit_depth * channels + 7) >> 3);
  126.    near_row_buf = (png_byte *)malloc((size_t)rowbytes);
  127.    row_buf = (png_bytep)near_row_buf;
  128.    if (!row_buf)
  129.    {
  130.       fprintf(STDERR, "No memory to allocate row buffer\n");
  131.       png_destroy_read_struct(&read_ptr, &info_ptr, &end_info);
  132.       png_destroy_write_struct(&write_ptr, (png_infopp)NULL);
  133.       fclose(fpin);
  134.       fclose(fpout);
  135.       return 1;
  136.    }
  137.  
  138.    if (info_ptr->interlace_type)
  139.    {
  140.       num_pass = png_set_interlace_handling(read_ptr);
  141.       num_pass = png_set_interlace_handling(write_ptr);
  142.    }
  143.    else
  144.    {
  145.       num_pass = 1;
  146.    }
  147.  
  148.    for (pass = 0; pass < num_pass; pass++)
  149.    {
  150.       for (y = 0; y < info_ptr->height; y++)
  151.       {
  152. #ifdef TESTING
  153.          fprintf(STDERR, "Processing line #%ld\n", y);
  154. #endif
  155.          png_read_rows(read_ptr, (png_bytepp)&row_buf, (png_bytepp)0, 1);
  156.          png_write_rows(write_ptr, (png_bytepp)&row_buf, 1);
  157.       }
  158.    }
  159.  
  160.    png_read_end(read_ptr, end_info);
  161.    png_write_end(write_ptr, end_info);
  162.  
  163.    png_destroy_read_struct(&read_ptr, &info_ptr, &end_info);
  164.    png_destroy_write_struct(&write_ptr, (png_infopp)NULL);
  165.  
  166.    fclose(fpin);
  167.    fclose(fpout);
  168.  
  169.    free((void *)near_row_buf);
  170.  
  171.    fpin = fopen(inname, "rb");
  172.  
  173.    if (!fpin)
  174.    {
  175.       fprintf(STDERR, "Could not find file %s\n", inname);
  176.       return 1;
  177.    }
  178.  
  179.    fpout = fopen(outname, "rb");
  180.    if (!fpout)
  181.    {
  182.       fprintf(STDERR, "Could not find file %s\n", outname);
  183.       fclose(fpin);
  184.       return 1;
  185.    }
  186.  
  187.    while (1)
  188.    {
  189.       int num_in, num_out;
  190.  
  191.       num_in = fread(inbuf, 1, 256, fpin);
  192.       num_out = fread(outbuf, 1, 256, fpout);
  193.  
  194.       if (num_in != num_out)
  195.       {
  196.          fprintf(STDERR, "Files %s and %s are of a different size\n",
  197.                  inname, outname);
  198.          fclose(fpin);
  199.          fclose(fpout);
  200.          return 1;
  201.       }
  202.  
  203.       if (!num_in)
  204.          break;
  205.  
  206.       if (memcmp(inbuf, outbuf, num_in))
  207.       {
  208.          fprintf(STDERR, "Files %s and %s are different\n", inname, outname);
  209.          fclose(fpin);
  210.          fclose(fpout);
  211.          return 1;
  212.       }
  213.    }
  214.  
  215.    fclose(fpin);
  216.    fclose(fpout);
  217.    fprintf(STDERR, "libpng passes test\n");
  218.  
  219.    return 0;
  220. }
  221.  
  222.